/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.action.equiposRed;

import com.mapping.Dispositivo;
import com.mapping.Modelo;
import com.mapping.TipoDisp;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value = "/")
@Action(value = "gestionAgregarEquipoRedPre", results = {
    @Result(name = "success", location = "/GestionEquiposRed/gestionAgregarEquipoRed.jsp")})
public class AgregarEquiposRedPre extends ActionSupport{
    private DetalleR[] detalles;
    private String idRed;
    
    @Override
    public String execute(){
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        
        TypedQuery  e=em.createQuery("SELECT d FROM Dispositivo d", Dispositivo.class);    
        List<Dispositivo> resultList = e.getResultList();
        ArrayList<DetalleR> Adeta = new ArrayList<DetalleR>();
        for(int i=0; i <resultList.size(); i++){                                             
            Dispositivo disp = resultList.get(i);
            Modelo mod = disp.getIdModelo();
            TipoDisp tipo = mod.getIdTipo();            
            if(validaTipo(tipo.getTipo())){
              DetalleR aux = new DetalleR();
              aux.setIdDispositivo(disp.getIdDisp());
              aux.setTipo(tipo.getTipo());
              Adeta.add(aux);
            }
        }       
        
        detalles = new DetalleR[Adeta.size()];
        for(int i=0; i<detalles.length; i++){
            detalles[i] = Adeta.get(i);
        }
        em.flush();
        tx.commit();
        em.close();  
        System.out.println("IdRed"+ this.getIdRed());
        return SUCCESS;
    }    

    private boolean validaTipo(String tipo) {
       if(tipo.equals("Impresora")){
           return true;
       }else if(tipo.equals("Hub")){
           return true;
       }else if(tipo.equals("Router")){
           return true;       
       }else if(tipo.equals("Switch")){
           return true;
       }else if(tipo.equals("PC")){
           return true;
       }else if(tipo.equals("Lap-Top")){
           return true;
       }else if(tipo.equals("Modem")){
           return true;
       }else if(tipo.equals("Notebook")){ 
           return true;
       } 
       return false;
    }

    public DetalleR[] getDetalles() {
        return detalles;
    }

    public String getIdRed() {
        return idRed;
    }

    public void setIdRed(String idRed) {
        this.idRed = idRed;
    }        
  
}
